package com.hyz.imageloader.utils;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.FileDescriptor;

/**
 * Created by yyy on 2018/4/9.
 *
 * decodeFile和decodeResource间接调用decodeStream
 *
 */

public class CompressPicture {


    /**
     * 从文件描述符进行decode
     * @param fd
     * @param reqWidth
     * @param reqHeight
     * @return
     */
    public static Bitmap decodeSampledBitmapFromFileDescriptor(FileDescriptor fd , int reqWidth , int reqHeight){

        //First decode width inJustDecodeBounds = true to cheack dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFileDescriptor(fd , null ,  options);
        //Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options , reqWidth , reqHeight);
        //Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFileDescriptor(fd , null ,  options);

    }


    /**
     * 从本地sdcard进行decode
     * @param path
     * @param reqWidth
     * @param reqHeight
     * @return
     */
    public static Bitmap decodeSampledBitmapFromFile(String path ,  int reqWidth , int reqHeight){

        //First decode width inJustDecodeBounds = true to cheack dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path ,  options);
        //Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options , reqWidth , reqHeight);
        //Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(path , options);

    }

    /**
     * 从res文件夹decode
     * @param res
     * @param resId
     * @param reqWidth
     * @param reqHeight
     * @return
     */
    public static Bitmap decodeSampledBitmapFromResource(Resources res , int resId , int reqWidth , int reqHeight){

        //First decode width inJustDecodeBounds = true to cheack dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res , resId , options);
        //Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options , reqWidth , reqHeight);
        //Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res , resId, options);

    }

    /**
     * 计算insampleSize的值，即采样率
     * @param options
     * @param reqWidth 加载图片的imageView的宽
     * @param reqHeight 加载图片的imageView的高
     * @return
     */
    private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        //Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if(height > reqHeight || width > reqWidth){
            final int halfWidth = width / 2;
            final int halfHeight = height / 2;
            //calculate the largest insamplesize value that is a power of 2 and keep both
            //height and width larger than the requested height and width
            while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth){
                inSampleSize *= 2;
            }

        }

        return inSampleSize;
    }
}
